package besta.moon.sql.expression.function.string;

import besta.moon.sql.parser.SQLTokenizer;
import besta.moon.sql.expression.function.FunctionReturnString;

public class FunctionSpace extends FunctionReturnString {

    @Override
    public final int getFunction() {
        return SQLTokenizer.SPACE;
    }

    @Override
    public boolean isNull() throws Exception {
        return param1.isNull() || param1.getInt() < 0;
    }

    @Override
    public final String getString() throws Exception {
        if (isNull()) {
            return null;
        }
        int size = param1.getInt();
        if (size < 0) {
            return null;
        }
        char[] buffer = new char[size];
        for (int i = 0; i < size; i++) {
            buffer[i] = ' ';
        }
        return new String(buffer);
    }

    @Override
    public final int getDataType() {
        return SQLTokenizer.VARCHAR;
    }
}
